/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 *
 * Contact luchyx@riaevolution.com
 *
 * See www.riaevolution.com
 * See www.riaevolution.com/blog
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * */

package com.riaevolution.google {
	import flash.events.Event;

	import mx.controls.Image;

	/**
	 * Provide acces for the static image map url generation.
	 *
	 * @example simple implementatation
	 * <listing>
	 * &lt;google:StaticMapImage apikey="{GOOGLE::key}"
	 * 			   id="mapimg"
	 * 				   width="320"
	 * 				   height="320"/&gt;
	 * </listing>
	 */
	public class StaticMapImage extends Image {
		public function StaticMapImage() {
			super();
			_staticmap = new StaticMap();
			_staticmap.addEventListener("urlChanged", onUrlChange, false, 0, true);
			onUrlChange(null);
		}

		/**
		 * Set dummy mode so this consume the NOT-BILLABLE url for images.
		 */
		public var dummy:Boolean = true;

		private function onUrlChange(event:Event):void {
			this.source = dummy ? _staticmap.testUrl : _staticmap.url;
			this.toolTip = _staticmap.url;
		}

		private var _staticmap:StaticMap;

		public function get mapapi():StaticMap {
			return _staticmap;
		}

		private var _zoom:Number;

		public function get zoom():Number {
			return _zoom;
		}

		public function set zoom(value:Number):void {
			_zoom = value;
			if (_staticmap) {
				_staticmap.zoomLevel = _zoom;
			}
		}


		/**
		 * Set the current marker
		 * @param value
		 */
		public function set markers(value:String):void {
			if (_staticmap) {
				_staticmap.markers = value;
			}
		}

		private var _apikey:String;

		public function get apikey():String {
			return _apikey;
		}

		public function set apikey(value:String):void {
			_apikey = value;
			if (_staticmap) {
				_staticmap.mapKey = value;
			}
		}


		override public function set width(value:Number):void {
			super.width = value;
			if (_staticmap) {
				_staticmap.width = value;
			}
		}

		override public function set height(value:Number):void {
			super.height = value;
			if (_staticmap) {
				_staticmap.height = value;
			}
		}
	}
}